<?php 
//输入一个链表，按链表值从尾到头的顺序返回一个ArrayList。

class ListNode{
    public $val;
    public $next = null;
    public function __construct($x){
        $this->val = $x;
    }
}

function printListFromTailToHead($head)
{
	if (is_null($head)) {
		return null;
	}
    $pre = null;
    $cur = $head;
    while ($cur != null) {
    	$next = $cur->next;
    	$cur->next = $pre;
    	$pre = $cur;
    	$cur = $next;
    }
    return $pre;
}
$head = new ListNode(1);
$head->next = new ListNode(2);
$head->next->next = new ListNode(3);
$head->next->next->next = new ListNode(4);
$head->next->next->next->next = new ListNode(5);

$node = printListFromTailToHead($head);
while ( !is_null($node) ) {
	echo $node->val.PHP_EOL;
	$node = $node->next;
}